package com.slkj.shilixiaoyuanapp.ui.common.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.ui.common.LookPicActivity__JumpCenter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created  on 2018/7/3 0003
 *
 * @author 唐开阔
 * @describe 选择图片适配器
 */
public class ChosePicAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private Context context;
    private ArrayList<String> picPaths = new ArrayList<>();
    //是否可以删除
    private boolean isCanDelete = false;

    public ChosePicAdapter(@Nullable ArrayList<String> picPaths, Context context) {
        this(picPaths, context, true);
    }

    public ChosePicAdapter(@Nullable ArrayList<String> picPaths, Context context, boolean isCanDelete) {
        super(R.layout.item_chose_pic, picPaths);
        this.context = context;
        this.isCanDelete = isCanDelete;
        this.picPaths = picPaths;
    }

    public ChosePicAdapter(@Nullable List<String> picPaths, Context context, boolean isCanDelete) {
        super(R.layout.item_chose_pic, picPaths);
        this.context = context;
        this.isCanDelete = isCanDelete;
        this.picPaths.addAll(picPaths);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        Glide.with(context).load(item)
                .fallback(R.drawable.ic_man)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_man)
                .into((ImageView) helper.getView(R.id.imageView));

        helper.setOnClickListener(R.id.img_delete, v -> {
                    picPaths.remove(helper.getAdapterPosition());
                    notifyItemRemoved(helper.getAdapterPosition());
                })
                .setVisible(R.id.img_delete, isCanDelete)
                .itemView.setOnClickListener(v ->
                        LookPicActivity__JumpCenter.builder(context)
                        .setImgUrls(picPaths)
                        .setStartPos(helper.getAdapterPosition())
                        .create().go()); }
}
